﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Starbucks.Messages;
using PServiceBus.MessageProxy;

namespace Starbucks.Service {

    class Program {
        static Random _random = new Random();
        static void Main(string[] args) {

           ProxyService.Start();

            var service = ProxyService.Create<IStarbuckService>();


            service.ReceiveDrinkPaymentProcessed(msg => {
                service.SendDrinkReady(new DrinkReady() { CustomerName = msg.CustomerName });
                Console.WriteLine("Barista: {0} is served :)", msg.CustomerName);
            }, ex => true);

            service.ReceiveDrinkPaymentMade(msg => {
                Console.WriteLine("Cashier: Received Payment for {0}'s Drink Order", msg.CustomerName);
                service.SendDrinkPaymentProcessed(new DrinkPaymentProcessed() { CustomerName = msg.CustomerName });
            }, ex => true);

            service.ReceiveNewDrinkOrder(msg => {
                var customerName = msg.CustomerName;
                var amount = (int)msg.Type * (decimal)1.0m * _random.Next(10) + 5;
                
                Console.WriteLine("Cashier: Received Drink Order for {0}", msg.CustomerName);

                service.SendPrepareDrink(new PrepareDrink() { 
                    CustomerName = customerName,
                    DrinkName = msg.Name, Type = msg.Type
                });

                
                service.SendDrinkPayment(new DrinkPayment() { 
                    Amount = amount,
                    CustomerName = customerName 
                });

                Console.WriteLine("Cashier: Requesting Payment..");
                service.SendNewDrinkOrderAccepted(new NewDrinkOrderAccepted() { CustomerName = customerName });
            }, ex => true);


            service.ReceivePrepareDrink(msg => {
                Console.WriteLine("Barista: Preparing {0} drink for {1}", msg.DrinkName, msg.CustomerName);
                service.SendDrinkPrepared(new DrinkPrepared() {
                    CustomerName = msg.CustomerName,
                    DrinkName = msg.DrinkName,
                    Type = msg.Type
                });
            }, ex => true);

            Console.WriteLine("Waiting for drink request.... Press enter to exit");

            Console.ReadLine();

            ProxyService.Shutdown();
        }
    }
}
